package com.atcx.controller;


import com.atcx.mapper.NoticesMapper;
import com.atcx.pojo.Classes;
import com.atcx.pojo.Notices;
import com.atcx.pojo.User;
import com.atcx.service.ClassesService;
import com.atcx.service.NoticesService;
import com.atcx.service.StudentClassService;
import com.atcx.service.UserService;
import com.atcx.util.PageResult;
import com.atcx.util.QueryPageBean;
import com.atcx.util.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pangShu
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/notices")
public class NoticesController {


    private Result result;
    @Resource
    private NoticesService noticesService;
    @Resource
    private UserService userService;
    @Resource
    private ClassesService classesService;
    @Resource
    private StudentClassService studentClassService;
    /**
     * @Author:pangshu
     * @Description: 添加公告信息
     * @Date:21:47 2022/3/23
     * @Param:
     * @return: com.atcx.util.Result
     **/
    @RequestMapping("addNotices")
    public Result addNotices(@RequestBody Notices notices,HttpServletRequest request){
        Integer userId = getUserId(request);
        User user = userService.getById(userId);
        notices.setPublish(user.getName());
        int i = noticesService.addNotices(notices);
        if (i>0){
            result = new Result(true,"添加公告信息成功");
        }else {
            result = new Result(false,"添加公告信息失败");
        }
        return result;
    }

    @RequestMapping("updateNotices")
    public Result updateNotices(@RequestBody Notices notices){
        int i = noticesService.updateNotices(notices);
        if (i>0){
            result = new Result(true,"修改公告信息成功");
        }else {
            result = new Result(false,"修改公告信息失败");
        }
        return result;
    };
    @RequestMapping("deleteNotices")
    public Result deleteNotices(int id){
        int i = noticesService.deleteNotices(id);
        if (i>0){
            result = new Result(true,"删除公告成功");
        }else {
            result = new Result(false,"删除公告失败,请先删除关联信息");
        }
        return result;
    }
    @RequestMapping("findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult page = noticesService.findByPage(queryPageBean);
        if (page != null){
            result = new Result(true,"查询公告信息成功",page);
        }else {
            result = new Result(false,"查询公告信息失败");
        }
        return result;
    }
    //根据当前用户信息查询公告列表
    @RequestMapping("findByPageWithUser")
    public Result findByPageWithUser(@RequestBody QueryPageBean queryPageBean,HttpServletRequest request){
        //设置查询条件为当前用户信息
        Integer userId = getUserId(request);
        User user = userService.getById(userId);
        queryPageBean.setQueryString(user.getName());
        PageResult page = noticesService.findByPageWithUser(queryPageBean);
        if (page != null){
            result = new Result(true,"查询公告信息成功",page);
        }else {
            result = new Result(false,"查询公告信息失败");
        }
        return result;
    }
    /**
     * @Author:pangshu
     * @Description: 获取公告信息显示到前端
     * @Date:21:59 2022/3/23
     * @Param:
     * @return: com.atcx.util.Result
     **/
    @RequestMapping("getNotice")
    public Result getNotice(){

        Notices notice = noticesService.getNotice();
        if (notice!=null){
            result = new Result(true,"查询公告信息成功",notice);
        }else {
            result = new Result(false,"查询公告信息失败");
        }
        return result;
    }
    //根据登录的老师的信息查询公告信息
    @RequestMapping("getNoticeByUser")
    public Result getNoticeByUser(HttpServletRequest request){
        Integer userId = getUserId(request);
        User user = userService.getById(userId);
        Notices notice = noticesService.getNoticeByUser(user.getName());
        if (notice!=null){
            result = new Result(true,"查询公告信息成功",notice);
        }else {
            result = new Result(false,"查询公告信息失败");
        }
        return result;
    }
    //根据当前用户的班级信息查询公告信息
    @RequestMapping("getNoticeByClass")
    public Result getNoticeByClass(HttpServletRequest request){
        Integer userId = getUserId(request);
        Integer classId = studentClassService.getClassId(userId);
        Classes classes = classesService.getClassesById(classId);
        Notices notice = noticesService.getNoticeByClass(classes.getTeacher());
        if (notice!=null){
            result = new Result(true,"查询公告信息成功",notice);
        }else {
            result = new Result(false,"查询公告信息失败");
        }
        return result;
    }

    public static Integer getUserId(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        Integer UserId = 0;
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("userId")){
                String value = cookie.getValue();
                UserId = Integer.parseInt(value);
                break;
            }
        }
        return UserId;
    }
}

